package com.health.system.common.utils;

import com.health.system.common.exception.GetRequestAttrFailException;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 */
public class ServletRequestUtil {
    private ServletRequestUtil() {

    }

    public static ServletRequestAttributes getServletRequestAttrs() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        if (attributes == null) {
            throw new GetRequestAttrFailException();
        }

        return attributes;
    }

    public static HttpSession getHttpSession() {
        return getRequest().getSession();
    }

    public static String getAttribute(String key) {
        return (String) getHttpSession().getAttribute(key);
    }

    public static HttpServletRequest getRequest() {
        return getServletRequestAttrs().getRequest();
    }
}
